﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YourMoney
{
    /// <summary>
    /// Форма для добавления нового типа сущности
    /// </summary>
    public partial class AddTypeForm : Form
    {
        public AddTypeForm() 
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
        }

        private void AddTypeForm_Load(object sender, EventArgs e)
        {
            treeView1.HideSelection = false;
            treeView1.Nodes.Clear();
            List<TreeNode> rootsList = StaticMembers.RootEssenceSelect();
            for (int i = 0; i < rootsList.Count; i++)
            {
                StaticMembers.SetEssenceTree(rootsList[i]);
                treeView1.Nodes.Add(rootsList[i]);

            }
            treeView1.ExpandAll();
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SQLiteDriver drv = SQLiteDriver.GetDrv("Money.db3");
            string cmd;
            TypeOfEssence newEssence;
            TypeOfEssence es = ((TypeOfEssence)treeView1.SelectedNode.Tag);
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                newEssence = new TypeOfEssence(es.ID, es.KindId, 0, textBox1.Text.ToString(), colorDialog1.Color.ToArgb());
            }
            else
            {
                newEssence = new TypeOfEssence(es.ID, es.KindId, 0, textBox1.Text.ToString(), es.Color);
            }
            cmd = @"INSERT INTO TypeOfEssence (parent_id,root_id,is_root,name,color) VALUES ('" + newEssence.ParentID + "','" + newEssence.KindId +
                "','" + newEssence.IsRoot + "','" + newEssence.Name + "','" + newEssence.Color + "');";
            drv.NonExecuteQuery(cmd);
            MessageBox.Show("Тип успешно добавлен!");
            this.Close();
            
        }

        private void AddTypeForm_Activated(object sender, EventArgs e)
        {
            
        }
    }
}
